<?php
/**
 * 货币（币种）（基础）
 * (model)
 */

namespace App\Models\Basics;

use Illuminate\Database\Eloquent\Model;
class Currency extends Model
{
    protected $table = 'b_currency';

    public $timestamps = false;

    protected $guarded = [];

    public static function options()
    {
        return self::query()->pluck('name','code');
    }

    public static function getNameByCode($code){
        return self::query()->where('code',$code)->value('name');
    }

    public static function existByCode($code){
        $count = self::query()->where('code',$code)->count();
        return $count > 0 ? true : false;
    }

    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($model){
            $count = ExchangeRate::query()->where('currency_code',$model->code)->count();
            if($count > 0){
                throw new \Exception('使用中勿删除');
            }
        });
    }
}
